package com.sansan.qiangji.realize.system;

import com.sansan.qiangji.VO.system.role.RoleVO;
import com.sansan.qiangji.entity.system.Menu;
import com.sansan.qiangji.entity.system.Power;
import com.sansan.qiangji.entity.system.Role;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.system.role.RoleMenuPowerEditForm;
import com.sansan.qiangji.service.impl.system.MenuServiceImpl;
import com.sansan.qiangji.service.impl.system.PowerServiceImpl;
import com.sansan.qiangji.service.impl.system.RoleServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class RoleReal {
    private RoleServiceImpl roleService;
    private MenuServiceImpl menuService;
    private PowerServiceImpl powerService;

    @Resource
    public void setRoleService(RoleServiceImpl roleService){
        this.roleService = roleService;
    }

    @Resource
    public void setMenuService(MenuServiceImpl menuService) { this.menuService = menuService; }

    @Resource
    public void setPowerService(PowerServiceImpl powerService) { this.powerService = powerService; }

    @Transactional
    public void createRole(String roleName){
        List<Role> roles = roleService.findByRoleName(roleName);
        if(roles.size()!=0){
            log.error("【创建角色】角色已存在，roleName={}", roleName);
            throw new ServiceException(ResultEnum.ROLE_EXIST);
        }
        Role role = new Role();
        role.setRoleName(roleName);
        role.setRoleStatus(true);
        roleService.addRole(role);
    }

    public List<RoleVO> getRoles(){
        List<RoleVO> roleVOS = new ArrayList<>();
        Role role = new Role();
        List<Role> roles = roleService.roleList(role);
        for (Role role1: roles){
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role1, roleVO);
            roleVOS.add(roleVO);
        }
        return roleVOS;
    }

    @Transactional
    public void deleteRole(Integer roleId){
        Role role = roleService.findByRoleId(roleId);
        if (role == null) {
            log.error("【删除角色】角色不存在");
            throw new ServiceException(ResultEnum.ROLE_NOT_EXIST);
        }
        int rows = roleService.deleteById(roleId);
        if (rows == 0) {
            log.error("【删除菜单】删除角色失败，roleId={}", roleId);
            throw new ServiceException(ResultEnum.ROLE_DELETE_ERROR);
        }
    }

    @Transactional
    public void changeRoleStatus(Integer roleId){
        Role role = roleService.findByRoleId(roleId);
        if (role == null){
            log.error("【更改角色状态】角色不存在，roleId={}", roleId);
            throw new ServiceException(ResultEnum.ROLE_NOT_EXIST);
        }
        role.setRoleStatus(!role.getRoleStatus());
        int rows = roleService.updateRole(role);
        if (rows == 0){
            log.error("【更改角色状态】更改角色状态失败，roleId={}", roleId);
            throw new ServiceException(ResultEnum.CHANGE_ROLE_STATUS_ERROR);
        }
    }

    @Transactional
    public void roleMenuPowerEdit(RoleMenuPowerEditForm roleMenuPowerEditForm) {
        Role role = roleService.findByRoleId(roleMenuPowerEditForm.getRoleId());
        if (role == null) {
            log.error("【角色菜单权限编辑】角色不存在");
            throw new ServiceException(ResultEnum.ROLE_NOT_EXIST);
        }
        List<Menu> menus = menuService.findAllMenu();
        StringBuilder roleMenus = new StringBuilder();
        for (String menuRoot : roleMenuPowerEditForm.getRoleMenuPowers()){
            if (menus.stream().anyMatch(e -> e.getMenuRoot().equals(menuRoot))) {
                if (roleMenus.toString().equals("")){
                    roleMenus = new StringBuilder(menuRoot);
                }else
                {
                    roleMenus.append("||").append(menuRoot);
                }
            }
        }
        role.setRoleMenus(roleMenus.toString());
        int rows = roleService.updateRole(role);
        if (rows == 0){
            log.error("【角色菜单权限编辑】角色菜单权限编辑失败，roleMenuPowerEditForm={}", roleMenuPowerEditForm.toString());
            throw new ServiceException(ResultEnum.ROLE_MENU_POWER_EDIT_ERROR);
        }
    }

    @Transactional
    public void rolePowerEdit(Integer roleId, String powerName) {
        Role role = roleService.findByRoleId(roleId);
        if (role == null) {
            log.error("【角色权限编辑】角色不存在");
            throw new ServiceException(ResultEnum.ROLE_NOT_EXIST);
        }
        List<Power> powers = powerService.findByPowerName(powerName);
        if (powers.size() == 0) {
            log.error("【角色权限编辑】权限不存在");
            throw new ServiceException(ResultEnum.POWER_NOT_EXIST);
        }
        List<String> rolePowers = new ArrayList<>(Arrays.asList(role.getRolePowers().split("\\|\\|")));
        if (!rolePowers.contains(powerName)){
            rolePowers.add(powerName);
        }else {
            rolePowers.remove(powerName);
        }
        role.setRolePowers(StringUtils.join(rolePowers, "||"));
        int rows = roleService.updateRole(role);
        if (rows == 0){
            log.error("【角色权限编辑】角色权限编辑失败");
            throw new ServiceException(ResultEnum.ROLE_POWER_EDIT_ERROR);
        }
    }
}
